import axios from "axios";

const dtOriginAxios = axios.create();

function addAuthorizationSearch (config) {
  let headers = config.headers;
  let token = sessionStorage.getItem("dtUseToken") || sessionStorage.getItem("token");
  if (token) {
    headers["Authorization"] = "Bearer " + token;
  } else {
    // config.data = config.data
    return config.data
  }
}

// axios默认配置
dtOriginAxios.defaults.timeout = 10000; // 超时时间
dtOriginAxios.defaults.baseURL = process.env.VUE_APP_URL;

// http request 拦截器
dtOriginAxios.interceptors.request.use(
  (config) => {
    config.headers["Content-Type"] = "application/json;charset=UTF-8";
    config.headers["Access-Control-Allow-Origin"] = "*";
    config.headers["Access-Control-Allow-Header"] = "Content-Type,*";
    addAuthorizationSearch(config);
    return config;
  },
  (error) => {
    return Promise.reject(error.response);
  }
);
// 设备管理
export default {
  getDeviceData (data) {
    return dtOriginAxios.post(`/open/api/device/search`, data)
  },
  getDtSetInHome (data) {
    return dtOriginAxios.get(`/open/api/device/setInHome/${data.deviceCode}/${data.inHome}`)
  },
  getExportTask (data) {
    return dtOriginAxios.post(`/open/api/export/task`, data)
  },
  // 导出任务总数
  getSearchCount (data) {
    return dtOriginAxios.post(`/open/api/export/task/searchCount`, data)
  },
  // 导出任务列表 
  getSearch (data) {
    return dtOriginAxios.post(`/open/api/export/task/search`, data)
  },
  // 下载
  getTaskStatus (data) {
    return dtOriginAxios.put(`/open/api/export/task/update/taskStatus`, data)
  },
  // 删除导出任务 
  clearExportTask (exportTaskId) {
    return dtOriginAxios.delete(`/open/api/export/task/${exportTaskId}`, { data: { exportTaskId } })
  },
  importExcel (file) {
    return dtOriginAxios.post(`/open/api/device/setInHome/readExcel`, file, {
      timeout: 200000
    });
  },
  batchInsert (filter) {
    return dtOriginAxios.post(`/open/api/device/batchInsert`, filter);
  },
  loadExcelTemplates () {
    return dtOriginAxios.get("/open/api/import/templates")
  },
  // getDtToken () {
  //   return axios.get(`/api/v1.0/getToken`)
  // }
};